<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/sample.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_ckeditor.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};
    Ext.onReady(function () {
        Ext.define('BranchList', {
            extend: 'Ext.data.Model',
            fields: [
                'branch_no','branch_name'
            ]
        });
        // create the Data Store
        var store = Ext.create('Ext.data.Store', {
            pageSize: 10,
            model: 'BranchList',
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/branch/all',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'count'
                },
                simpleSortMode: true
            }
        });

        var combo = new Ext.form.field.ComboBox({
            fieldLabel : "营业部选择",
            store: store,
            name : 'branch_no',
            valueField: 'branch_no',
            displayField: 'branch_name',
            colspan:3,
            listeners : {
                'beforequery':function(e){
                    var combo = e.combo;
                    if(!e.forceAll){
                        var input = e.query;
                        // 检索的正则
                        var regExp = new RegExp(".*" + input + ".*");
                        // 执行检索
                        combo.store.filterBy(function(record,id){
                            var branch_no = record.get('branch_no');
                            var branch_name = record.get('branch_name');
                            return regExp.test(branch_no) || regExp.test(branch_name);
                        });
                        combo.expand();
                        return false;
                    }
                },
                'select' : function( combo, records, eOpts ){
                    //console.log(records);
                    var branch_no = records[0].data.branch_no;
                    var branch_name = records[0].data.branch_name;
                    Ext.Ajax.request({
                        url: '/branch/list',
                        params : {branch_no:branch_no,branch_name:branch_name},
                        method : 'get',
                        success: function(response, opts) {
                            var _data =  Ext.decode(response.responseText);
                            data = _data.data[0];
                            init_form();
                        },
                        failure: function(response, opts) {
                            alert('获取营业部信息失败');
                        }
                    });
                }
            }
        });
        //保存数据
        function doAdd(){
            var params = dataForm.form.getFieldValues();
            if(params._id == ""){
                alert("请通过下拉列表选择营业部，营业部信息不得为空!");
                return;
            }
            _url = '/branch/edit';
            if(params.image_file_id){
                params.image_url = '/file/download?file_id=' + params.image_file_id;
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert('业务处理成功');
                },
                failure: function(response, opts) {
                    alert('业务处理失败');
                }
            });

        }

        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'}
        ];

        dataForm = new Ext.FormPanel({
            layout: 'fit',
            items: [
                {
                    xtype: 'form',
                    defaultType: 'textfield',
                    layout: {type: 'table',columns:3},
                    items: [
                        {name : '_id',hidden : true},
                        {name : 'image_url',hidden : true},
                        {name : 'create_by',hidden : true},
                        {name : 'create_tel',hidden : true},
                        {name : 'branch_name',hidden : true},
                        combo,
                        {fieldLabel: '省份',name : 'province',readOnly:true},
                        {fieldLabel: '城市',name : 'city',readOnly:true},
                        {fieldLabel: '地址',name : 'address'},
                        {fieldLabel: '经度',name : 'lng',xtype : 'numberfield',hidden:false},
                        {fieldLabel: '纬度',name : 'lat',xtype : 'numberfield',hidden:false},
                        {fieldLabel: '联系方式',name : 'tel',colspan:2},
                        {fieldLabel: '图标id',name: 'image_file_id',readOnly : true},
                        {xtype: "filefield",fieldLabel: '营业部图片',name: 'attach_file'},
                        {
                            xtype: "button",
                            text: "上传营业部图片",
                            width: 100,
                            handler: function () {
                                uploadFile(dataForm);
                            },
                            style: 'margin-left: 25px;'
                        },
                        {emptyText : '营业部描述',height:200,name : 'introduce',xtype:'textareafield',
                         width:'100%',anchor: '100%',colspan:4}
                    ],
                    tbar : tbar
                }
            ]
        });
        var viewport = new Ext.Viewport({
            renderTo: "mypanel",
            items: [dataForm]
        });



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/branch/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                alert("未找到相关营业部信息");
                return ;
            }
            dataForm.form.reset();
            dataForm.form.setValues(data);
            //经纬度特殊
            if(dataForm.form.findField("lng")){
                if(data.loc.length == 2){
                    dataForm.form.findField("lng").setValue(data.loc[0]);
                    dataForm.form.findField("lat").setValue(data.loc[1]);
                }
            }
        }
    });
</script>
<body>
<div>
    <h1>请通过下拉列表选择自己所在的一个营业部，并修改相关的营业部信息！</h1>
</div>
<div id="mypanel" style="height:1024px;width:100%"></h1>
</div>

</body>
</html>